package com.example.pokemon_chat_room.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.PokemonDetail;
import com.example.pokemon_chat_room.R;
import com.example.pokemon_chat_room.pojo.AvatarBase;
import com.example.pokemon_chat_room.pojo.Pokemon;
import com.example.pokemon_chat_room.util.MyImageView;

import java.util.List;

public class AvatarAdapter extends RecyclerView.Adapter<AvatarAdapter.ViewHolder> {

    public static final String ACTION_RECEIVE_MESSAGE = "com.example.pokemon_chat_room.AVATAR";
    public static final String KEY_RECEIVED_DATA = "data";


    private List<AvatarBase> avatarBaseList;
    private View view;
    private Context context;

    public AvatarAdapter(Context context, List<AvatarBase> avatarBaseList) {
        this.context = context;
        this.avatarBaseList = avatarBaseList;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private MyImageView avatar_img;

        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View

            avatar_img = view.findViewById(R.id.avatar_img);
        }

    }


    @NonNull
    @Override
    public AvatarAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_item, parent, false);
        AvatarAdapter.ViewHolder holder = new AvatarAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull AvatarAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.avatar_img.setImageURL("poke/ava/" + avatarBaseList.get(position).getImage());


        holder.avatar_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessageBroadcast(avatarBaseList.get(position).getImage());
            }
        });
    }

    @Override
    public int getItemCount() {
        return avatarBaseList.size();
    }

    private void sendMessageBroadcast(String message){
        if (!message.isEmpty()){
            Intent intent = new Intent();
            intent.setAction(ACTION_RECEIVE_MESSAGE);
            intent.putExtra(KEY_RECEIVED_DATA, message);
            context.sendBroadcast(intent);
        }
    }
}
